package web.protocols.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.InvestigadorProtocoloCentroService;
import services.OngoingService;
import services.ProtocolCRUDService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 06/11/12
 * Time: 11:11
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/Ongoing")
public class OngoingController extends ActionSupport{

    private ArrayList<Involucrado> principalInvestigators = new ArrayList<Involucrado>(0);
    private LinkedHashMap<Integer, String> listPrincipalInvestigators = new LinkedHashMap<Integer, String>(0);
    private ArrayList<String> statuses = new ArrayList<String>(0);
    private ArrayList<String> localApprovedBy = new ArrayList<String>(0);
    private ArrayList<String> inspectionsMOH = new ArrayList<String>(0);
    private ArrayList<String> inspectionActions = new ArrayList<String>(0);
    private ArrayList<String> outcomes = new ArrayList<String>(0);
    private OngoingService ongoingService;
    private InvestigadorProtocoloCentroService investigadorProtocoloCentroService;
    private ProtocolCRUDService protocolCRUDService;
    private Integer investigatorId;

    private Ongoing ongoing;
    private InputStream siteName;
    private Integer plannedPtsEnrolled;
    private Integer plannedPtsRandomized;
    private Date plannedFPFV;
    private Date plannedLPFV;
    private Date plannedLPLV;

    private ArrayList<Ongoing> listOngoing = new ArrayList<Ongoing>(0);

    private boolean view;
    private Integer editableId;

    @Autowired
    public OngoingController(OngoingService ongoingService, InvestigadorProtocoloCentroService investigadorProtocoloCentroService, ProtocolCRUDService protocolCRUDService) {
        this.ongoingService = ongoingService;
        this.investigadorProtocoloCentroService = investigadorProtocoloCentroService;
        this.protocolCRUDService = protocolCRUDService;
    }

    @Action(value="main", results={
            @Result(name="success", location="/protocols.ongoing.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        ProtocoloCompromisopais pcp = protocolCRUDService.recoverProtocoloCompromisopais();
        initDummyLists(pcp);

        EnrolledPatients ep = getLastEnrolledPatients(pcp.getEnrolledPatients());

        plannedPtsRandomized = ep != null? ep.getTargetEnrolledAmount() : null;
        plannedPtsEnrolled = ep != null? ep.getTargetEnrolledRandomized() : null;
        plannedFPFV = pcp.getPlannedFpfv();
        plannedLPFV = pcp.getPlannedLpfv();
        plannedLPLV = pcp.getPlannedLpfv();

        listOngoing = (ArrayList<Ongoing>) ongoingService.findAll();

        if(editableId != null){
            ongoing = ongoingService.findById(editableId);
        }

        return SUCCESS;
    }

    private EnrolledPatients getLastEnrolledPatients(List<EnrolledPatients> enrolledPatients) {
        EnrolledPatients ep = null;
        for(EnrolledPatients aux : enrolledPatients){
            if(ep == null || aux.getPatientOrder() > ep.getPatientOrder())
                ep = aux;
        }
        return ep;
    }

    @Action(value="getSiteName", results={
            @Result(name="success", type = "stream", params = { "contentType", "text/hmtl", "inputName", "siteName",})
    })
    public String getSite(){
        try {
            InvestigadorProtocoloCentro investigadorProtocoloCentro = investigadorProtocoloCentroService.findByInvestigatorAndProtocoloCompromisoPais(protocolCRUDService.recoverProtocoloCompromisopais(), investigatorId);
            if(investigadorProtocoloCentro == null){
                siteName = new ByteArrayInputStream( "Unknown".getBytes());
            }
            else{
                String data = investigadorProtocoloCentro.getProtocoloCentro().getCentro().getInstitucion() + "-" + investigadorProtocoloCentro.getProtocoloCentro().getCentro().getStatus();
                siteName = new ByteArrayInputStream( data.getBytes());
            }
        } catch (NoProtocoloCompromisopaisSelectedException e) {}
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        ProtocoloCompromisopais pcp = protocolCRUDService.recoverProtocoloCompromisopais();

        ongoingService.save(pcp, ongoing, investigatorId);

        Map session = ActionContext.getContext().getSession();

        if(pcp.getCompromisopais().getCountry().toUpperCase().equals("ARGENTINA")){
            if(pcp.getStatus() != null){
                session.put("statusargentina", "(" + pcp.getStatus() + ")");
            }else{
                session.put("statusargentina", "");
            }
        }
        if(pcp.getCompromisopais().getCountry().toUpperCase().equals("BRASIL") || pcp.getCompromisopais().getCountry().toUpperCase().equals("BRAZIL")){
            if(pcp.getStatus() != null){
                session.put("statusbrazil", "(" + pcp.getStatus() + ")");
            }else{
                session.put("statusbrazil", "");
            }
        }
        if(pcp.getCompromisopais().getCountry().toUpperCase().equals("COLOMBIA")) {
            if(pcp.getStatus() != null){
                session.put("statuscolombia", "(" + pcp.getStatus() + ")");
            }else{
                session.put("statuscolombia", "");
            }
        }
        if(pcp.getCompromisopais().getCountry().toUpperCase().equals("PERU")){
            if(pcp.getStatus() != null){
                session.put("statusperu", "(" + pcp.getStatus() + ")");
            }else{
                session.put("statusperu", "");
            }
        }
        if(pcp.getCompromisopais().getCountry().toUpperCase().equals("CHILE")){
            if(pcp.getStatus() != null){
                session.put("statuschile", "(" + pcp.getStatus() + ")");
            }else{
                session.put("statuschile", "");
            }
        }

        return SUCCESS;
    }

    private void initDummyLists(ProtocoloCompromisopais pcp) {
        statuses.add("Start Up");
        statuses.add("Enrolling");
        statuses.add("Enrollment Closed");
        statuses.add("Closed");

        localApprovedBy.add("COIES");
        localApprovedBy.add("CCIS");
        localApprovedBy.add("CABA");
        localApprovedBy.add("COPEIS");
        localApprovedBy.add("Other");

        inspectionsMOH.add("Local");
        inspectionsMOH.add("FDA");
        inspectionsMOH.add("EMA");
        inspectionsMOH.add("Other");

        inspectionActions.add("No Action");
        inspectionActions.add("IVA");
        inspectionActions.add("IOA");

        outcomes.add("No findings");
        outcomes.add("Minor findings");
        outcomes.add("Major findings");
        outcomes.add("Critical findings");

        for(Involucrado i : investigadorProtocoloCentroService.findPrincipalInvestigatorsByProtocol(pcp)){
            listPrincipalInvestigators.put(i.getId(), i.getNombre() + " " + i.getApellido());
        }
    }

    public ArrayList<Involucrado> getPrincipalInvestigators() {
        return principalInvestigators;
    }

    public void setPrincipalInvestigators(ArrayList<Involucrado> principalInvestigators) {
        this.principalInvestigators = principalInvestigators;
    }

    public ArrayList<String> getStatuses() {
        return statuses;
    }

    public void setStatuses(ArrayList<String> statuses) {
        this.statuses = statuses;
    }

    public ArrayList<String> getLocalApprovedBy() {
        return localApprovedBy;
    }

    public void setLocalApprovedBy(ArrayList<String> localApprovedBy) {
        this.localApprovedBy = localApprovedBy;
    }

    public ArrayList<String> getInspectionsMOH() {
        return inspectionsMOH;
    }

    public void setInspectionsMOH(ArrayList<String> inspectionsMOH) {
        this.inspectionsMOH = inspectionsMOH;
    }

    public ArrayList<String> getInspectionActions() {
        return inspectionActions;
    }

    public void setInspectionActions(ArrayList<String> inspectionActions) {
        this.inspectionActions = inspectionActions;
    }

    public ArrayList<String> getOutcomes() {
        return outcomes;
    }

    public void setOutcomes(ArrayList<String> outcomes) {
        this.outcomes = outcomes;
    }

    public LinkedHashMap<Integer, String> getListPrincipalInvestigators() {
        return listPrincipalInvestigators;
    }

    public void setListPrincipalInvestigators(LinkedHashMap<Integer, String> listPrincipalInvestigators) {
        this.listPrincipalInvestigators = listPrincipalInvestigators;
    }

    public Integer getInvestigatorId() {
        return investigatorId;
    }

    public void setInvestigatorId(Integer investigatorId) {
        this.investigatorId = investigatorId;
    }

    public Ongoing getOngoing() {
        return ongoing;
    }

    public void setOngoing(Ongoing ongoing) {
        this.ongoing = ongoing;
    }

    public InputStream getSiteName() {
        return siteName;
    }

    public void setSiteName(InputStream siteName) {
        this.siteName = siteName;
    }

    public Integer getPlannedPtsEnrolled() {
        return plannedPtsEnrolled;
    }

    public void setPlannedPtsEnrolled(Integer plannedPtsEnrolled) {
        this.plannedPtsEnrolled = plannedPtsEnrolled;
    }

    public Date getPlannedLPFV() {
        return plannedLPFV;
    }

    public void setPlannedLPFV(Date plannedLPFV) {
        this.plannedLPFV = plannedLPFV;
    }

    public Integer getPlannedPtsRandomized() {
        return plannedPtsRandomized;
    }

    public void setPlannedPtsRandomized(Integer plannedPtsRandomized) {
        this.plannedPtsRandomized = plannedPtsRandomized;
    }

    public Date getPlannedFPFV() {
        return plannedFPFV;
    }

    public void setPlannedFPFV(Date plannedFPFV) {
        this.plannedFPFV = plannedFPFV;
    }

    public Date getPlannedLPLV() {
        return plannedLPLV;
    }

    public void setPlannedLPLV(Date plannedLPLV) {
        this.plannedLPLV = plannedLPLV;
    }

    public ArrayList<Ongoing> getListOngoing() {
        return listOngoing;
    }

    public void setListOngoing(ArrayList<Ongoing> listOngoing) {
        this.listOngoing = listOngoing;
    }

    public boolean isView() {
        return view;
    }

    public void setView(boolean view) {
        this.view = view;
    }

    public Integer getEditableId() {
        return editableId;
    }

    public void setEditableId(Integer editableId) {
        this.editableId = editableId;
    }
}
